//
//  BillServiceViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/26.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class BillServiceViewController : BillRootViewController {
    
    fileprivate let billButton = UIButton() //账单
    fileprivate let serviceButton = UIButton() //服务
    
    fileprivate let selectView = UIView() //滚动条
    
    fileprivate let scrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()

        addSubView()
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        self.view.backgroundColor = UIColor.mineColor()
        
        billButton.backgroundColor = UIColor.white
        billButton.setTitle(bill, for: .normal)
        billButton.setTitleColor(UIColor.red, for: .normal)
        billButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        billButton.addTarget(self, action: #selector(selectType(sender:)), for: .touchUpInside)
        self.view.addSubview(billButton)
        
        serviceButton.backgroundColor = UIColor.white
        serviceButton.setTitle(service_title, for: .normal)
        serviceButton.setTitleColor(UIColor.black, for: .normal)
        serviceButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        serviceButton.addTarget(self, action: #selector(selectType(sender:)), for: .touchUpInside)
        self.view.addSubview(serviceButton)
        
        selectView.backgroundColor = UIColor.red
        self.view.addSubview(selectView)
        
        scrollView.backgroundColor = UIColor.white
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        self.view.addSubview(scrollView)
        
        let billVC = BillDetailsViewController()
        self.addChildViewController(billVC)
        self.didMove(toParentViewController: self)
        scrollView.addSubview(billVC.view)
        
        let serviceVC = ServiceDetailsViewController()
        self.addChildViewController(serviceVC)
        self.didMove(toParentViewController: self)
        scrollView.addSubview(serviceVC.view)
        
        billButton.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.equalTo(ScreenW/2-0.5)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        serviceButton.snp.makeConstraints { (make) in
            make.right.top.equalToSuperview()
            make.width.height.equalTo(billButton)
        }
        
        selectView.snp.makeConstraints { (make) in
            make.bottom.centerX.equalTo(billButton)
            make.width.equalTo(ScreenW/4)
            make.height.equalTo(RATIO_H(maxNum: 2))
        }
        
        scrollView.snp.makeConstraints { (make) in
        make.top.equalTo(billButton.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.bottom.equalToSuperview()
        }

        billVC.view.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(self.view)
        make.top.equalTo(billButton.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.width.equalToSuperview()
        }

        serviceVC.view.snp.makeConstraints { (make) in
            make.top.bottom.width.equalTo(billVC.view)
            make.left.equalTo(billVC.view.snp.right)
        }

        self.view.layoutIfNeeded()

        scrollView.contentSize = .init(width: ScreenW*2, height: 0)
    }
    
    //MARK: 选择类型
    @objc fileprivate func selectType(sender:UIButton) {
        
        switch sender.titleLabel?.text{
        case service_title:
            billButton.setTitleColor(UIColor.black, for: .normal)
            serviceButton.setTitleColor(UIColor.red, for: .normal)
            
            UIView.animate(withDuration: 0.2) {
                self.selectView.center.x = self.serviceButton.center.x
                self.scrollView.contentOffset.x = ScreenW
            }
            
        case bill:
            billButton.setTitleColor(UIColor.red, for: .normal)
            serviceButton.setTitleColor(UIColor.black, for: .normal)
            
            UIView.animate(withDuration: 0.2) {
                self.selectView.center.x = self.billButton.center.x
                self.scrollView.contentOffset.x = 0
            }
        default:
            break
        }
    }

}

extension BillServiceViewController : UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        if scrollView.contentOffset.x == ScreenW {
            billButton.setTitleColor(UIColor.black, for: .normal)
            serviceButton.setTitleColor(UIColor.red, for: .normal)
            selectView.center.x = serviceButton.center.x
        }else if scrollView.contentOffset.x == 0 {
            billButton.setTitleColor(UIColor.red, for: .normal)
            serviceButton.setTitleColor(UIColor.black, for: .normal)
            selectView.center.x = billButton.center.x
        }
        
        selectView.frame.origin.x = scrollView.contentOffset.x/2 + ScreenW/8
        
        if selectView.center.x > ScreenW/2 {
            billButton.setTitleColor(UIColor.black, for: .normal)
            serviceButton.setTitleColor(UIColor.red, for: .normal)
        }else{
            billButton.setTitleColor(UIColor.red, for: .normal)
            serviceButton.setTitleColor(UIColor.black, for: .normal)
        }
    }
}
